package com.sky.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author majun
 * @package com.sky.utils
 * @date 2024/11/21 15:47
 */
@Component
public class EmailCreateAnAccountUtil {

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 渲染邮件模板内容
     * @param templateName 模板名称
     * @param variables 动态变量
     * @return 渲染后的HTML内容
     */
    public String renderTemplate(String templateName, Map<String, Object> variables) {
        try {
            Context context = new Context();
            context.setVariables(variables);
            return templateEngine.process(templateName, context);
        } catch (Exception e) {
            // 可以记录日志或抛出自定义异常
            throw new RuntimeException("邮件模板渲染失败: " + templateName, e);
        }
    }

}
